from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from datetime import datetime
from interview.models import Candidate
from interview.admin_utils import get_detailpage_fieldsets, get_listdisplay_fields
from django.db.models import Q
# Register your models here.


def action_update_major(modeladmin, request, queryset):
    queryset.update(major='y')

def action_update_hr(modeladmin, request, queryset):
    queryset.update(hr_interviewer_user = request.user)




action_update_major.short_description = "将选择的候选人专业改为y"
action_update_hr.short_description = "将选择的候选人hr改为登录者"
action_update_hr.allowed_permissions = ('kkkk',)

@admin.register(Candidate)
class CandidateAdmin(ImportExportModelAdmin):
    exclude = ('creator', 'created_date', 'modified_date')

    actions = [action_update_major, action_update_hr]



   # list_display = (
   #      'username', 'city', 'bachelor_school','first_score', 'first_result', 'first_interviewer_user', 'second_score',
   #      'second_result', 'second_interviewer_user', 'hr_score', 'hr_result', 'hr_interviewer_user',)



    fieldsets = (
        ("基本", {'fields':("userid",("username", "city", "phone"), ("email", "apply_position", "born_address"), "gender", "candidate_remark", "bachelor_school", "master_school", "doctor_school", "major", "degree", "test_score_of_general_ability", "paper_score", "last_editor",)}),
        ("一面", {'fields':("first_score", "first_learning_ability", "first_professional_competency", "first_advantage", "first_disadvantage", "first_result", "first_recommend_position", "first_interviewer_user", "first_remark",)}),
        ("二面", {'fields': ("second_score", "second_learning_ability", "second_professional_competency", "second_pursue_of_excellence", "second_communication_ability", "second_pressure_score", "second_advantage", "second_disadvantage", "second_result", "second_recommend_position", "second_interviewer_user", "second_remark",)}),
        ("HR面", {'fields': ("hr_score", "hr_responsibility", "hr_communication_ability", "hr_logic_ability", "hr_potential", "hr_stability", "hr_advantage", "hr_disadvantage", "hr_result", "hr_interviewer_user", "hr_remark",)}),
    )

    def has_kkkk_permission(self, request):
        opts = self.opts
        has_p = request.user.has_perm("%s.%s" % (opts.app_label, "notify"))
        print(has_p)
        return has_p

    def has_import_permission(self, request):
        group_names = [x.name for x in request.user.groups.all()]
        if 'interviewer' in group_names:
            return False
        else:
            return True


    search_fields = ('username', 'phone', 'bachelor_school')

    list_filter = ('city', 'first_result', 'second_result', 'hr_result')

    #ordering = ('hr_result', 'second_result')

    #list_editable = ('second_interviewer_user', 'first_interviewer_user')
    default_list_editable = ('second_interviewer_user', 'first_interviewer_user')

    def get_actions(self, request):
        actions = super(CandidateAdmin, self).get_actions(request)
        if 'hr' in [x.name for x in request.user.groups.all()]:
            actions.pop('action_update_major')
        print(actions)
        return actions


    def get_fieldsets(self, request, obj=None):
        return get_detailpage_fieldsets(request.user, obj)

    def get_list_editable(self, request):
        group_names = [x.name for x in request.user.groups.all()]
        editable_fields = []
        if 'hr' in group_names or request.user.is_superuser:
            editable_fields.extend(self.default_list_editable)
        return editable_fields

    def get_changelist_instance(self, request):
        self.list_editable = self.get_list_editable(request)
        return super(CandidateAdmin, self).get_changelist_instance(request)

    def get_readonly_fields(self, request, obj=None):
        group_names = [x.name for x in request.user.groups.all()]
        readonly_fields = []
        if 'interviewer' in group_names:
            readonly_fields.extend(self.default_list_editable)
        return readonly_fields

    def get_queryset(self, request):
        qs = super(CandidateAdmin, self).get_queryset(request)
        if 'hr' in [x.name for x in request.user.groups.all()] or request.user.is_superuser:
            return qs
        return qs.filter(Q(second_interviewer_user = request.user) | Q(first_interviewer_user = request.user))

    def get_list_display(self, request):
        return get_listdisplay_fields(request.user)

    def save_model(self,request, obj, form, change):
        obj.last_editor = request.user.username
        if not obj.creator:
            obj.creator = request.user.username
        obj.modified_date = datetime.now()
        obj.save()